package com.wonder.common.utils.rsa;

import java.util.Map;

public class RSATester {

	static String publicKey;
	static String privateKey;

	static {
		try {
			Map<String, Object> keyMap = RSAUtils.genKeyPair();
			publicKey = RSAUtils.getPublicKey(keyMap);
			privateKey = RSAUtils.getPrivateKey(keyMap);
			System.err.println("公钥: \n\r" + publicKey);
			System.err.println("私钥： \n\r" + privateKey);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws Exception {
		//test();
		testSign();
	}

	static void test() throws Exception {
		System.err.println("公钥加密——私钥解密");
		String source = "{\"saleid\": \"10001000\",\"saleman\": \"营业员\",\"syman\": \"制单人\",\"customer\": \"客户信息\",\"storename\": \"库房\",\"creatdate\": \"2014-05-11 11:11:11\",\"batchdate\": \"2014-05-11 11:15:11\",\"productArr\": [{\"itemnumber\": \"商品编码_1.1\",\"quantity\": \"2\",\"saleprice\": \"500\",\"amount\": \"1000\"},{\"itemnumber\": \"商品编码_1.2\",\"quantity\": \"2\", \"saleprice\": \"500\", \"amount\": \"1000\"}],\"paymentArr\": [ \"支付信息一：2000\",\"支付信息二：3000\",\"支付信息三：4000\"]}";
		System.out.println("\r加密前文字：\r\n" + source);
		byte[] data = source.getBytes();
		byte[] encodedData = RSAUtils.encryptByPublicKey(data, publicKey);
		System.out.println("加密后文字：\r\n" + new String(encodedData));
		byte[] decodedData = RSAUtils.decryptByPrivateKey(encodedData,
				privateKey);
		String target = new String(decodedData);
		System.out.println("解密后文字: \r\n" + target);
	}

	static void testSign() throws Exception {
		System.err.println("私钥加密——公钥解密");
		String source = "86112b38f129d950c60f06ecd379e7733aab83efefa60e4912c4c36ae5b9aee1";
		
		String encodedString=RSAUtils.encryptByPrivateKeyWithBase64(source.getBytes(), privateKey);
		System.out.println("加密后：\r\n" + encodedString);
		
		String decodeString=new String(RSAUtils.decryptByPublicKeyWithBase64(encodedString, publicKey));
		System.out.println("解密后:：\r\n" + decodeString);
		
//		System.out.println("原文字：\r\n" + source);
//		byte[] data = source.getBytes();
//		byte[] encodedData = RSAUtils.encryptByPrivateKey(data, privateKey);
//		System.out.println("加密后：\r\n" + new String(encodedData));
//		
//		String encodeDataBase64=Base64Utils.encode(encodedData);
//		System.out.println("加密后BASE64ENCODE：\r\n"+encodeDataBase64);
//
//		byte[] decodeDataBase64=Base64Utils.decode(encodeDataBase64);
//		System.out.println("加密后BASE64DECODE：\r\n"+new String(decodeDataBase64));
//
//		
//		byte[] decodedData = RSAUtils.decryptByPublicKey(encodedData, publicKey);
//		String target = new String(decodedData);
//		System.out.println("解密后: \r\n" + target);
//		System.err.println("私钥签名——公钥验证签名");
//		String sign = RSAUtils.sign(encodedData, privateKey);
//		System.err.println("签名:\r" + sign);
//		boolean status = RSAUtils.verify(encodedData, publicKey, sign);
//		System.err.println("验证结果:\r" + status);
	}
}
